//
//  CWImageView.m
//  CWDevelop
//
//  Created by LittoCats on 7/22/14.
//  Copyright (c) 2014 Littocats. All rights reserved.
//

#import "CWImageView.h"
#import "CWBaseView.h"
#import "CWBaseLayout.h"
#import "NSObject+CW.h"

#import "UIImageView+AFNetworking.h"

@implementation CWImageView

- (CGSize)sizeThatFits:(CGSize)size
{
    CGSize defaultSize = [super sizeThatFits:CGSizeZero];
    CGSize reSize;
    
    reSize.width = [self.attributes[@"width"] isEqualToString:@"wrap_content"] ? defaultSize.width : self.width;
    reSize.height = [self.attributes[@"height"] isEqualToString:@"wrap_content"] ? defaultSize.height : self.height;
    return reSize;
}


- (void)setImageSource:(NSString *)image
{
    if ([[image lowercaseString] hasSuffix:@"http://"]){
        NSString *_id = self._id;
        NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:image]];
        [self setImageWithURLRequest:imageRequest
                    placeholderImage:[UIImage imageNamed:@"CWImageViewPlaceHolder"]
                             success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                                 [[NSObject findInstanceWithId:_id] setImage:image];
                                 [[NSObject findInstanceWithId:_id] sizeThatFits:CGSizeZero];
                             }
                             failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                 ;
                             }];
    }
}
@end
